package com.ttbj.controller.salesman;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ttbj.api.dto.PermissionDto;
import com.ttbj.api.dto.RoleDto;
import com.ttbj.api.dto.SalesmanDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.salesman.PermissionService;
import com.ttbj.api.salesman.RoleService;
import com.ttbj.api.salesman.SalesmanService;
import com.ttbj.constant.Const;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.util.ViewDataUtil;


@Controller
public class PermissionController {

    private Logger logger = Logger.getLogger(PermissionController.class);

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private RoleService roleService;

    
    @ResponseBody
    @RequestMapping(value="/sys/permission/info.htm", produces={"text/html;charset=UTF-8;","application/json;charset=UTF-8;"})
    private String getMenus(HttpServletRequest request) {
        SalesmanDto mandto = (SalesmanDto) request.getSession().getAttribute(Const.TTBJ_USER_SESSION_KEY);
        if (null == mandto) {
            return ViewDataUtil.buildFailureJson(PlatFormErrorCode.USER_SESSION_TIMEOUT.code(), "登录超时");
        }
        try {
        	SalesmanDto salesmanDto = salesmanService.querySalesmanById(mandto.getSalesmanId());
            RoleDto roleDto = roleService.querySalemanRoleById(mandto.getSalesmanId());
            List<PermissionDto> permissionDtos = permissionService.queryPermissionByRoleId(roleDto.getId());
            if (permissionDtos == null){
                permissionDtos = new ArrayList<PermissionDto>();
            }
            List<PermissionDto> topMenu = new ArrayList<PermissionDto>();
            List<PermissionDto> childMenu = null;
            for (PermissionDto permission : permissionDtos) {
                if (permission.getTopMenu() == 1) {
                    topMenu.add(permission);
                }
            }
            for (int i = 0, size = topMenu.size(); i < size; i++) {
                childMenu = new ArrayList<PermissionDto>();
                for (int j = 0, csize = permissionDtos.size(); j < csize; j++) {
                    if(permissionDtos.get(j).getMenuType() != 1){
                        continue;
                    }
                    if (permissionDtos.get(j).getParentId() != topMenu.get(i).getId()) {
                        continue;
                    }
                    childMenu.add(permissionDtos.get(j));
                }
                topMenu.get(i).setChildMenu(childMenu);
            }
            salesmanDto.setPermissionDtoList(topMenu);
            salesmanDto.setRoleDto(roleDto);
            salesmanDto.setSalesmanPwd(null);
            return ViewDataUtil.buildSuccessJson("菜单", salesmanDto);
        } catch (BusinessException be) {
            logger.error(be.getMessage(), be);
            return ViewDataUtil.buildFailureJson(be.getErrorCode(), be.getMessage());
        }
    }

}
